﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTcms.Common;

using DTcms.Model;
using System.Data;
using Newtonsoft.Json;
using DTcms.API.Payment.wxpay;
namespace DTcms.JOB.member_order.command
{
    public class Member_orderCommand
    {
        public async Task<string> run()
        {
            try
            {
                Utils.WriteTextLog("syncorderstatus", "检查微信支付状态:" , DateTime.Now);
                //string order_no = DTRequest.GetString("order_no");
                //string filename = "";
                DAL.member_order ubll = new DAL.member_order();
                Utils.WriteTextLog("syncorderstatus", "11111:", DateTime.Now);
                //DataSet ds = ubll.GetList(10, "payment_status !=1 and order_no='" + order_no + "' and datediff(minute,add_time,getdate())<120", " id desc");//payment_status !=1 and
                DataSet ds = ubll.GetList(10, "payment_status !=1  and datediff(minute,add_time,getdate())<1200", " id desc");//payment_status !=1 and
                Utils.WriteTextLog("syncorderstatus", "22222:", DateTime.Now);
                DataTable dt = ds.Tables[0];
                Utils.WriteTextLog("syncorderstatus", "获取订单条数:"+ dt.Rows.Count, DateTime.Now);
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        Utils.WriteTextLog("syncorderstatus"+ dr["order_no"].ToString(), "_order_no:" + dr["order_no"].ToString() + "提交时间" + dr["add_time"].ToString(), DateTime.Now);
                        //第一步先生成一个签名
                        string sendUrl = "https://api.mch.weixin.qq.com/pay/orderquery";

                        WxPayData data = new WxPayData();
                        data.SetValue("out_trade_no", dr["order_no"].ToString()); //商户订单号      111
                        data.SetValue("appid", "wxf3b482c702bdb291"); //公众账号ID  111
                        data.SetValue("mch_id", "1504256031"); //商户号             111
                        data.SetValue("nonce_str", GenerateNonceStr()); //随机字符串111

                        data.SetValue("sign", data.MakeSign("N8iTCQt8Ob2Fw6Yud4VIRnNaXJHllEfN")); //签名111

                        string xml = data.ToXml(); //转换成XML
                        Utils.WriteTextLog("get_orderid", "data" + xml, DateTime.Now);
                        string response = HttpService.Post(xml, sendUrl, false, 6); //发送请求
                        WxPayData wd = new WxPayData();
                        wd.FromXml(response);
                        Utils.WriteTextLog("syncorderstatus" + dr["order_no"].ToString(), "查询结果:" + response, DateTime.Now);
                        if (wd.GetValue("return_code").ToString() == "SUCCESS" && wd.GetValue("result_code").ToString() == "SUCCESS")
                        {
                            var paystate = wd.GetValue("trade_state").ToString();
                            Utils.WriteTextLog("syncorderstatus" + dr["order_no"].ToString(), "支付状态:" + paystate, DateTime.Now);
                            if (paystate == "SUCCESS")
                            {
                                Utils.WriteTextLog("syncorderstatus" + dr["order_no"].ToString(),"修改订单开始" , DateTime.Now);
                                ubll.UpdateField(int.Parse(dr["id"].ToString()), "payment_status=1,payment_time=getdate()");
                                DAL.users uu = new DAL.users("dt_");
                                DAL.company cc = new DAL.company();
                                int ucount=uu.UpdateField(Convert.ToInt32(dr["user_id"].ToString()), "group_id=" + dr["group_id"].ToString() + ",upgrade_time=getdate()");
                                Utils.WriteTextLog("syncorderstatus" + dr["order_no"].ToString(), "修改用户数量:" + ucount, DateTime.Now);
                                cc.UpdateFields(Convert.ToInt32(dr["user_id"].ToString()), "group_id=" + dr["group_id"].ToString() + ",upgrade_time=getdate()");
                                Utils.WriteTextLog("syncorderstatus" + dr["order_no"].ToString(), "修改订单:" + dr["order_no"].ToString() + "；支付状态：" + paystate, DateTime.Now);
                            }
                        }

                    }
                }
                return "success";
            }
            catch (Exception ex)
            {
                Utils.WriteTextLog("syncorderstatus", "错误信息:" + JsonHelper.ObjectToJSON(ex), DateTime.Now);
                return "F:"+ex.Message;
            }
        }
        /**
     * 生成随机串，随机串包含字母或数字
     * @return 随机串 
     */
        public string GenerateNonceStr()
        {
            return Guid.NewGuid().ToString().Replace("-", "");
        }
    }
}
